package com.wgz.ai.langchain4jai.memory;

import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.MemoryId;
import dev.langchain4j.service.UserMessage;

/**
 * @Title: WGZ
 * @Package com.wgz.ai.langchain4jai
 * @Date 2025/3/4 23:04
 * @description: 每个用户的独立聊天记忆。
 */
public class ServiceWithMemoryForEachUserExample {
    private final static String apiKey = "sk-svcacct-J-zoI616zoIN8ghM6amdG_SbFNc3vADd5yRa-53EQkr-lPN0tjD8QgWJfAmSuT3BlbkFJxBhbhopzngM8ZUB8KwByRFPnJ5bDUT1eq2Kop0OlggeqfzdsNSF45DQFZ94mgA";

    interface Assistant {

        String chat(@MemoryId int memoryId, @UserMessage String userMessage);
    }

        public static void main(String[] args) {

            ChatLanguageModel mode = OpenAiChatModel.builder()
                    .apiKey(apiKey)
                    .modelName("gpt-3.5-turbo")
                    .build();

            Assistant assistant = AiServices.builder(Assistant.class)
                    .chatLanguageModel(mode)
                    .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
                    .build();



            System.out.println(assistant.chat(1, "Hello, my name is Klaus"));
            // Hi Klaus! How can I assist you today?

            System.out.println(assistant.chat(2, "Hello, my name is Francine"));
            // Hello Francine! How can I assist you today?

            System.out.println(assistant.chat(1, "What is my name?"));
            // Your name is Klaus.

            System.out.println(assistant.chat(2, "What is my name?"));
            // Your name is Francine.
        }
}
